<?php
App::import('Controller','Comments');
App::import('Controller','Users');
App::import('Controller','Likes');

class PostsController extends AppController{
	public $helper = array('Html','Form');
	
    public function isAuthorized($user) {
    // All registered users can add posts
      if ($this->action === 'add'||$this->action=='view') {
        return true;
      }

  // The owner of a post can edit and delete it
      if (in_array($this->action, array('edit', 'delete'))) {
        $postId = $this->request->params['pass'][0];
        if ($this->Post->isOwnedBy($postId, $user['id'])) {
            return true;
        }
      }

       return parent::isAuthorized($user);
    }
	
	
	
	public function index(){
	    $this->set('posts',$this->Post->find('all'));	
	}
	
	public function view($id=NULL){
		if (!$id) {
			throw new NotFoundException(__('Invalid post'));
		}
		else{
			$post = $this->Post->findById($id);
			if (!$post) {
					throw new NotFoundException(__('Invalid post'));;
			}
			$this->set('post',$post);
			
			$userControl = new UsersController();
			$userControl->constructClasses();
			$userpost = $userControl->getUsernameById($post['Post']['user_id']);
			$this->set('userpost',$userpost);
			
			$likeControl = new LikesController();
			$likeControl->constructClasses();
			$likeCount = $likeControl->likeCount($id);
			$this->set('likeCount',$likeCount);
			$likeOrNot = $likeControl->likeOrNot($this->Auth->user('id'),$id);
			$this->set('likeOrNot',$likeOrNot);
			
			$commentControl = new CommentsController();
			$commentControl->constructClasses();
			$comments  = $commentControl->getCommentByPostId($id);
			$this->set('comments',$comments);
			

		}
	}
	
	public function add(){
		if ($this->request->is('post')) {
			$this->Post->create();
			$this->request->data['Post']['user_id'] = $this->Auth->user('id');
			date_default_timezone_set('Asia/Ho_Chi_Minh');
            $date = date('Y/m/d H:i:s', time());
			$this->request->data['Post']['create_time'] = $date;
			$this->request->data['Post']['modifie_time'] = $date;
			//debug($this->request->data);die;
			
			if ($this->Post->save($this->request->data)) {
			  $this->Session->setFlash('Your post has been saved');
			  $this->redirect(array('action'=>'index'));
		    }
		    else{
		      $this->Session->setFlash('Unable to save your post');
		    }
		}
	}

	
	
    public function edit($id = null) {
      if (!$id) {
        throw new NotFoundException(__('Invalid post'));
      }

      $post = $this->Post->findById($id);
      if (!$post) {
        throw new NotFoundException(__('Invalid post'));
      }

      if ($this->request->is('post') || $this->request->is('put')) {
        $this->Post->id = $id;
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $date = date('Y/m/d H:i:s', time());
	    $this->request->data['Post']['modifie_time'] = $date;
        if ($this->Post->save($this->request->data)) {
            $this->Session->setFlash('Your post has been updated.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to update your post.');
        }
      }

      if (!$this->request->data) {
        $this->request->data = $post;
      }
    }
    
    
    public function delete($id) {
       if ($this->request->is('get')) {
         throw new MethodNotAllowedException();
       }

       if ($this->Post->delete($id)) {
         $this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
         $this->redirect(array('action' => 'index'));
       }
    }
 
}